<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
/* eslint-disable @typescript-eslint/no-namespace */

// ***********************************************
// This commands.ts shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************

// ***********************************************
// Begin Specific Selector Attributes for Cypress
// ***********************************************

// Navbar
export const navbarSelector = '[data-cy="navbar"]';
export const adminMenuSelector = '[data-cy="adminMenu"]';
export const accountMenuSelector = '[data-cy="accountMenu"]';
export const registerItemSelector = '[data-cy="register"]';
export const settingsItemSelector = '[data-cy="settings"]';
export const passwordItemSelector = '[data-cy="passwordItem"]';
export const loginItemSelector = '[data-cy="login"]';
export const logoutItemSelector = '[data-cy="logout"]';
export const entityItemSelector = '[data-cy="entity"]';

<%_ if (!authenticationTypeOauth2) { _%>
// Login
export const titleLoginSelector = '[data-cy="loginTitle"]';
export const errorLoginSelector = '[data-cy="loginError"]';
export const usernameLoginSelector = '[data-cy="username"]';
export const passwordLoginSelector = '[data-cy="password"]';
export const forgetYourPasswordSelector = '[data-cy="forgetYourPasswordSelector"]';
export const submitLoginSelector = '[data-cy="submit"]';

// Register
export const usernameRegisterSelector = '[data-cy="username"]';
export const emailRegisterSelector = '[data-cy="email"]';
export const firstPasswordRegisterSelector = '[data-cy="firstPassword"]';
export const secondPasswordRegisterSelector = '[data-cy="secondPassword"]';
export const submitRegisterSelector = '[data-cy="submit"]';

// Settings
export const firstNameSettingsSelector = '[data-cy="firstname"]';
export const lastNameSettingsSelector = '[data-cy="lastname"]';
export const emailSettingsSelector = '[data-cy="email"]';
export const submitSettingsSelector = '[data-cy="submit"]';

// Password
export const currentPasswordSelector = '[data-cy="currentPassword"]';
export const newPasswordSelector = '[data-cy="newPassword"]';
export const confirmPasswordSelector = '[data-cy="confirmPassword"]';
export const submitPasswordSelector = '[data-cy="submit"]';

// Reset Password
export const emailResetPasswordSelector = '[data-cy="emailResetPassword"]';
export const submitInitResetPasswordSelector = '[data-cy="submit"]';
<%_ } _%>

// Administration
export const userManagementPageHeadingSelector = '[data-cy="userManagementPageHeading"]';
export const swaggerFrameSelector = 'iframe[data-cy="swagger-frame"]';
export const swaggerPageSelector = '[id="swagger-ui"]';
<%_ if (withAdminUi) { _%>
export const metricsPageHeadingSelector = '[data-cy="metricsPageHeading"]';
export const healthPageHeadingSelector = '[data-cy="healthPageHeading"]';
export const logsPageHeadingSelector = '[data-cy="logsPageHeading"]';
export const configurationPageHeadingSelector = '[data-cy="configurationPageHeading"]';
<%_ } _%>

// ***********************************************
// End Specific Selector Attributes for Cypress
// ***********************************************

export const classInvalid = <% if (clientFrameworkAngular) { %>'ng-invalid';<% } else { %>'is-invalid'<% } %>;

export const classValid = <% if (clientFrameworkAngular) { %>'ng-valid'<% } else { %>'is-valid'<% } %>;

Cypress.Commands.add('authenticatedRequest', (data) => {
<%_ if (authenticationTypeJwt) { _%>
  <%_ if (clientFrameworkAngular || clientFrameworkReact) { _%>
  const jwtToken = sessionStorage.getItem(Cypress.env('jwtStorageName'));
  const bearerToken = jwtToken && JSON.parse(jwtToken);
  <%_ } else { _%>
  const bearerToken = sessionStorage.getItem(Cypress.env('jwtStorageName'));
  <%_ } _%>
  if (bearerToken) {
    return cy.request({
      ...data,
      auth: {
        bearer: bearerToken,
      },
    });
  }
  return cy.request(data);
<%_ } else { _%>
  return cy.getCookie('XSRF-TOKEN').then(csrfCookie => {
    return cy.request({
        ...data,
        headers: {
          ...data.headers,
          'X-XSRF-TOKEN': csrfCookie?.value,
        },
    });
  });
<%_ } _%>
});

Cypress.Commands.add('login', (username: string, password: string) => {
  cy.session(
    [username, password],
    () => {
<%_ if (authenticationTypeOauth2) { _%>
      cy.getOauth2Data();
      cy.get('@oauth2Data').then(oauth2Data => {
        cy.oauthLogin(oauth2Data, username, password);
      });
<%_ } else { _%>
      cy.request({
        method: 'GET',
        url: '/api/account',
        failOnStatusCode: false,
      });
      cy.authenticatedRequest({
        method: 'POST',
        body: { username, password },
        url: Cypress.env('authenticationUrl'),
  <%_ if (authenticationTypeSession) { _%>
        form: true,
  <%_ } else { _%>
      }).then(({ body: { id_token } }) => {
    <%_ if (clientFrameworkVue) { _%>
        sessionStorage.setItem(Cypress.env('jwtStorageName'), id_token);
    <%_ } else { _%>
        sessionStorage.setItem(Cypress.env('jwtStorageName'), JSON.stringify(id_token));
    <%_ } _%>
  <%_ } _%>
      });
<%_ } _%>
    },
    {
      validate() {
        cy.authenticatedRequest({ url: '/api/account' }).its('status').should('eq', 200);
      },
    }
  );
});

declare global {
  namespace Cypress {
    interface Chainable {
      authenticatedRequest(data): Cypress.Chainable;
      login(username: string, password: string): Cypress.Chainable;
    }
  }
}

<%_ if (cypressAudit) { _%>
import 'cypress-audit/commands';
<%_ } _%>
// Convert this to a module instead of a script (allows import/export)
export {};
